local npc_cards_sys = Class(function(self, inst)
    self.inst = inst
    self.card_name = nil
    self.last_card_name = nil

    inst:ListenForEvent("npc_cards_sys.clicked",function()
        inst.npc_base_lib:RPC_AddTags({"npc_cards_sys.ready"})
        self:event_start_by_name(self.card_name)
        self.last_card_name = self.card_name
        self.card_name = nil
    end)
    inst:DoTaskInTime(0.1,function()
        inst.npc_base_lib:RPC_AddTags({"npc_cards_sys.ready"})
    end)

    -- inst:DoTaskInTime(3,function() ------- 带着事件穿越洞穴或者退出是不允许的。
    --     if self.card_name then
    --         print("npc_cards_sys onload check :",self.card_name)
    --         self:event_start_by_name(self.card_name)
    --         self.card_name = nil
    --     end
    -- end)

    self.cards = {
        ["npc_card_spring"] = 10,
        ["npc_card_summer"] = 10,
        ["npc_card_autumn"] = 10,
        ["npc_card_winter"] = 10,
        ----------- 1%
        ----- 4%
        
        ["npc_card_supplies"] = 350,
        ----- 35%
        ["npc_card_monsters"] = 300,
        ----- 30%
        ["npc_card_bottle_with_coin"] = 20,
        ----- 2%


        ["npc_card_freeze"] = 70,            
        ["npc_card_lightning"] = 70,
        ["npc_card_meteorite"] = 70,
        ["npc_card_reaper"] = 10,
        ["npc_card_teleportation"] = 70
        ----- 29%
    }

    if TUNING.NPC_CONFIG.TELEPORT_CARD ~= true then
        self.cards.npc_card_teleportation = 0
        self.cards.npc_card_supplies = self.cards.npc_card_supplies + 40
        self.cards.npc_card_monsters = self.cards.npc_card_monsters + 30
    end

    self.card_events = {}
    



end,
nil,
{

})
---------------------------------------------------------------------------------------------------------------------------------
local function season_card_check(card)
    local season_cards = {
        ["npc_card_spring"] = true,
        ["npc_card_summer"] = true,
        ["npc_card_autumn"] = true,
        ["npc_card_winter"] = true,
    }

    if season_cards[tostring(card)] then
        if TheWorld.state.isspring and card == "npc_card_spring" then
            return "npc_card_summer"
        elseif TheWorld.state.issummer and card == "npc_card_summer" then
            return "npc_card_autumn"
        elseif TheWorld.state.isautumn and card == "npc_card_autumn" then
            return "npc_card_winter"
        elseif TheWorld.state.iswinter and card == "npc_card_winter" then
            return "npc_card_spring"
        end
    else
        return card
    end
end
---------------------------------------------------------------------------------------------------------------------------------
function npc_cards_sys:Get_Random_Card()
    -------------------------------------------------------
    --- 为了让新添加的卡牌给玩家尽快抽到，特别做了这段函数
    local new_card = "npc_card_bottle_with_coin"
    if self.inst.npc_everything_data:Add("npc_cards_sys.num_cards",0) > 3 then
        if math.random(100) < 30 then
            if self.inst.npc_everything_data:Get("npc_cards_sys.new_card") ~= new_card then
                self.inst.npc_everything_data:Set("npc_cards_sys.new_card",new_card)
                return new_card
            end
        end
    end
    -------------------------------------------------------
    if self.card_events == nil or #self.card_events == 0 then
        self.card_events = {}
        for i = 1, 500, 1 do
            for name, num in pairs(self.cards) do
                if num ~= 0 then
                    table.insert(self.card_events,name)
                    self.cards[name] = self.cards[name] - 1
                end
            end
        end
    end
    self.inst.npc_everything_data:Add("npc_cards_sys.num_cards",1)  --- 记录总共抽卡多少次
    local ret_card_name =  self.card_events[math.random(#self.card_events)]
    if ret_card_name == new_card then
        self.inst.npc_everything_data:Set("npc_cards_sys.new_card",new_card)
    end



    ret_card_name = season_card_check(ret_card_name)



    return ret_card_name
end
---------------------------------------------------------------------------------------------------------------------------------
function npc_cards_sys:event_start_by_name(card_name)
    print("info:card_event_start_by_name",card_name)
    pcall(function()
        self.inst:SpawnChild(card_name):PushEvent("Set",self.inst)
    end)
end
---------------------------------------------------------------------------------------------------------------------------------
function npc_cards_sys:Send_RPC(card_name)
    self.card_name = card_name
    SendModRPCToClient(CLIENT_MOD_RPC["NPC_RPC_SYSTEM"]["npc_cards_sys.start"],self.inst.userid,self.inst,card_name)    
end

function npc_cards_sys:Start_test(card_name)    --- 开始下发卡牌。
    if not self.inst:HasTag("npc_cards_sys.ready") then
        return
    end
    self:Send_RPC(card_name or "npc_card_test")
    self.inst.npc_base_lib:RPC_RemoveTags({"npc_cards_sys.ready"})
end

function npc_cards_sys:Start()
    local card_name = self:Get_Random_Card()
    
    if card_name == nil then    ----- 
        card_name = "npc_card_supplies"
        print("error : npc_cards_sys:Start() and get Card name nil")
    else
        print("info : npc_cards_sys:Start() and get Card name :",card_name)
    end

    self:Start_test(card_name)
end
---------------------------------------------------------------------------------------------------------------------------------
-- function npc_cards_sys:OnSave()
--     local data =
--     {
--         cards_sys_card_name = self.card_name
--     }
--     return next(data) ~= nil and data or nil
-- end

-- function npc_cards_sys:OnLoad(data)
--     if data.cards_sys_card_name then
--         self.card_name = data.cards_sys_card_name
--     end
-- end



return npc_cards_sys